﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace WebShopData
{
    public class OrderRow
    {
        public int OrderRowID { get; set; }
        public int OrderID { get; set; }
        public int ArticleID { get; set; }
        public int Quantity { get; set; }

        public void Load(SqlDataReader reader)
        {
            OrderRowID = int.Parse(reader["OrderRowID"].ToString());
            OrderID = int.Parse(reader["OrderID"].ToString());
            ArticleID = int.Parse(reader["ArticleID"].ToString());
            Quantity = int.Parse(reader["Quantity"].ToString());
        }

        public OrderRow(int articleID, int quantity, int orderID, int orderRowID = 0)
        {
            OrderRowID = orderRowID;
            OrderID = orderID;
            ArticleID = articleID;
            Quantity = quantity;
        }

        public OrderRow(int articleID, int quantity, int orderRowID = 0)
        {
            OrderRowID = orderRowID;
            ArticleID = articleID;
            Quantity = quantity;
        }

        public OrderRow()
        {

        }

        public override string ToString()
        {
            return string.Format("OrderRowID:{0} OrderID:{1} ArticleID:{2} Quantity:{3}", OrderRowID, OrderID, ArticleID, Quantity);
        }
    }

    public static class OrderRows
    {
        public static int AddOrderRow(OrderRow orderRow, int orderId)
        {
            try
            {
                using (SqlConnection connection = DB.GetConnection())
                {
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = @"INSERT INTO OrderRow(OrderID,ArticleID,Quantity) VALUES ({0},{1},{2})";
                        command.CommandText = string.Format(command.CommandText, orderId, orderRow.ArticleID, orderRow.Quantity);

                        int affectedrows = command.ExecuteNonQuery();
                        return affectedrows;
                    }
                }
            }
            catch (Exception ex)
            {
                DBException.AddException(ex.Message, ex.GetType().ToString());
                throw new Exception();
            }
        }

        public static OrderRow GetOrderRow(int orderID)
        {
            try
            {
                var orderrow = new OrderRow();
                using (SqlConnection connection = DB.GetConnection())
                {
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = @"SELECT * FROM OrderRow WHERE OrderID = {0}";
                        command.CommandText = string.Format(command.CommandText, orderID);
                        SqlDataReader reader = command.ExecuteReader();
                        if (reader.Read())
                        {
                            orderrow.Load(reader);
                        }
                    }
                }
                return orderrow;
            }
            catch (Exception)
            {

                throw new Exception();
            }
        }
        public static List<OrderRow> GetOrderRows(int orderID)
        {
            List<OrderRow> orderRows = new List<OrderRow>();
            using (SqlConnection connection = DB.GetConnection())
            {
                using (SqlCommand command = connection.CreateCommand())
                {
                     command.CommandText = string.Format("SELECT * FROM OrderRow WHERE OrderID = {0}",orderID);
                        var adapter = new SqlDataAdapter(command);
                        var dataSet = new DataSet();
                        adapter.Fill(dataSet);

                        var order = Orders.GetOrder(orderID);
                        if (order != null)
                        {
                            for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
                            {
                                orderRows.Add( new OrderRow(
                                    orderRowID:int.Parse(dataSet.Tables[0].Rows[i]["OrderRowID"].ToString()),
                                    orderID: int.Parse(dataSet.Tables[0].Rows[i]["OrderID"].ToString()),
                                    articleID:int.Parse(dataSet.Tables[0].Rows[i]["ArticleID"].ToString()),
                                    quantity:int.Parse(dataSet.Tables[0].Rows[i]["Quantity"].ToString())                     
                                    ));
                            }
                            return orderRows;
                        }
                        else
                        {
                            throw new Exception();
                        }
                }
            }
        }
    }


}
